package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.WmSensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-03
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    /**
     * 敏感词新增
     * @param wmSensitive
     */
    @Override
    public ResponseResult mgcsave(WmSensitive wmSensitive) {
        //1.敏感词为空
        if (wmSensitive.getSensitives()==null){
            return ResponseResult.errorResult(1,"敏感词为空");
        }
        //定义条件构造器
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        //编写条件
        queryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        //查询
        WmSensitive one = this.getOne(queryWrapper);
        if (one !=null){
            return ResponseResult.errorResult(1,"敏感词已存在");
        }
        this.save(wmSensitive);
        return ResponseResult.okResult("敏感词新增成功");
    }

    /**
     * 敏感词分页模糊查询
     * @param wmSensitiveDto 条件
     * @return
     */
    @Override
    public ResponseResult mgclist(WmSensitiveDto wmSensitiveDto) {
        //构建分页对象 Page
        Page<WmSensitive> page = new Page<>(wmSensitiveDto.getPage(), wmSensitiveDto.getSize());
        //定义条件构造器
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //编写条件语句
        if (StringUtils.isNotBlank(wmSensitiveDto.getName())){
            queryWrapper.like(WmSensitive::getSensitives,wmSensitiveDto.getName());
        }
            page = this.page(page, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult<>(wmSensitiveDto.getPage(), wmSensitiveDto.getSize(), page.getTotal(), page.getRecords());
        return responseResult;
    }

    /**
     * 敏感词修改
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult mgcalter(WmSensitive wmSensitive) {
        //1.创建条件构造器
        //1.1查询构造器
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(wmSensitive.getSensitives())){
            queryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
            WmSensitive one = this.getOne(queryWrapper);
            if (one != null){
                return ResponseResult.errorResult(1,"敏感词重复请重新修改!");
            }else {
                this.updateById(wmSensitive);
                return ResponseResult.okResult();
            }
        }else {
            return ResponseResult.errorResult(1,"修改的敏感词为空请输入敏感词!");
        }
    }
}
